//
//  TTNavigationController.swift
//  NewWeiBo
//
//  Created by 吴天益 on 2017/7/22.
//  Copyright © 2017年 ttt. All rights reserved.
//

import UIKit

class TTNavigationController: UINavigationController,UIGestureRecognizerDelegate {
    
    

    override func viewDidLoad() {
        super.viewDidLoad()

        //设置pop手势代理
        self.interactivePopGestureRecognizer?.delegate = self
    }
    

    //MARK: - 重写控制器压栈方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        super.pushViewController(viewController, animated: true)
        
        //第一个push的控制器
        if(viewControllers.count == 2){

            //为我的
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "我的", target: self, action: #selector(popController), imageName: "navigationbar_back_withtext")
            
            viewController.navigationItem.title = String(format: "第%d级控制器", viewControllers.count)
            
            
        }
        
        if(viewControllers.count > 2){

            //后面的为返回
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", target: self, action: #selector(popController), imageName: "navigationbar_back_withtext")
            
            viewController.navigationItem.title = String(format: "第%d级控制器", viewControllers.count)
            
        }
        
    }
    

    //MARK: - 监听方法
    func popController(){
        
        self.popViewController(animated: true)

    }
    
    
    //MARK: - 重新开启pop手势(pop手势代理方法)
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
